; ---------------------------------------------------------------------------

sample_entry	struc ;	(sizeof=0xA)
playing:	db ?
id:		db ?
bank:		db ?			; string(C)
offset:		dw ?
length:		dw ?
length_high_byte:db ?
length_low_word:dw ?
sample_entry	ends


;
; +-------------------------------------------------------------------------+
; |   This file	has been generated by The Interactive Disassembler (IDA)    |
; |	      Copyright	(c) 2011 Hex-Rays, <support@hex-rays.com>	    |
; +-------------------------------------------------------------------------+
;
; Input	MD5   :	593656C38B49B369F54438C2BB08B459
; Input	CRC32 :	5676C0BE

; File Name   :	Z:\home\marzojr\Documents\Source\sonicretro\smps\smps-ristar-2.bin
; Format      :	Binary file
; Base Address:	0000h Range: 0000h - 00E9h Loaded length: 00E9h

; Processor	  : z80	[]
; Target assembler: Zilog Macro	Assembler

; ===========================================================================

; Segment type:	Regular
		segment	ROM

; =============== S U B	R O U T	I N E =======================================

; Attributes: noreturn

EntryPoint:
		di
		di
		di
		ld	a, 2Ah
		ex	af, af'


PCMwaitloop:				; CODE XREF: EntryPoint+Aj
					; EntryPoint+89j
		ld	a, (driver_input)
		or	a
		jr	z, PCMwaitloop

		ld	(fm_in_progress_flag), a


busywait:				; CODE XREF: EntryPoint+13j
		ld	a, (zYM2612_A0)
		or	a
		jp	m, busywait

		ld	a, 2Bh
		ld	(zYM2612_A0), a
		ld	a, 80h
		ld	(zYM2612_D0), a
		xor	a
		ld	(fm_in_progress_flag), a


PCMplayloop:				; CODE XREF: EntryPoint+5Cj
		xor	a
		ld	(driver_input),	a
		ld	ix, sample0
		call	LoadSampleData

		ld	hl, driver_input
		exx
		ld	bc, fm_in_progress_flag
		ld	de, zYM2612_D0
		ld	hl, zYM2612_A0
		exx


PCMplaysampleloop:			; CODE XREF: EntryPoint+59j
					; EntryPoint+6Ej
		ld	a, e
		or	d
		jr	z, samplebankdone

		ld	a, (bc)
		dec	de
		inc	bc
		ex	af, af'
		exx
		ld	(bc), a


busywait_1:				; CODE XREF: EntryPoint+49j
		bit	7, (hl)
		jr	nz, busywait_1

		ld	(hl), a
		ret	nz
		ex	af, af'
		ld	(de), a
		xor	a
		ld	(bc), a
		exx
		ld	a, 1


pitchloop:				; CODE XREF: EntryPoint+55j
		dec	a
		jr	nz, pitchloop

		ld	a, (hl)
		or	a
		jp	p, PCMplaysampleloop

		jp	PCMplayloop

; ---------------------------------------------------------------------------

samplebankdone:				; CODE XREF: EntryPoint+3Fj
		ld	ix, sample0
		ld	a, (ix+sample_entry.length_low_word)
		or	(ix+ sample_entry.length_low_word+1 )
		jr	z, disableDAC

		call	DoNextSampleBank

		jp	PCMplaysampleloop

; ---------------------------------------------------------------------------

disableDAC:				; CODE XREF: EntryPoint+69j
		ld	a, 80h
		ld	(fm_in_progress_flag), a


busywait_2:				; CODE XREF: EntryPoint+7Aj
		ld	a, (zYM2612_A0)
		or	a
		jp	m, busywait_2

		ld	a, 2Bh
		ld	(zYM2612_A0), a
		xor	a
		ld	(zYM2612_D0), a
		ld	(fm_in_progress_flag), a
		jp	PCMwaitloop

; End of function EntryPoint


; =============== S U B	R O U T	I N E =======================================


LoadSampleData:				; CODE XREF: EntryPoint+2Cp
		call	zBankSwitch

		ld	(ix+sample_entry.id), a
		ld	d, (ix+sample_entry.length)
		ld	e, (ix+	sample_entry.length+1 )
		ld	b, (ix+sample_entry.offset)
		ld	c, (ix+	sample_entry.offset+1 )
		ret

; End of function LoadSampleData


; =============== S U B	R O U T	I N E =======================================


DoNextSampleBank:			; CODE XREF: EntryPoint+6Bp
		inc	(ix+sample_entry.bank)
		call	zBankSwitch

		ld	bc, zROMWindow
		ld	a, (ix+sample_entry.length_low_word)
		rla
		ld	a, (ix+sample_entry.length_high_byte)
		rl	a
		jr	z, dolastbank

		ld	a, (ix+sample_entry.length_low_word)
		sub	80h
		ld	(ix+sample_entry.length_low_word), a
		ld	a, (ix+sample_entry.length_high_byte)
		sbc	a, 0
		ld	(ix+sample_entry.length_high_byte), a
		ld	de, zROMWindow
		ret

; ---------------------------------------------------------------------------

dolastbank:				; CODE XREF: DoNextSampleBank+12j
		ld	d, (ix+sample_entry.length_low_word)
		ld	e, (ix+	sample_entry.length_low_word+1 )
		ld	(ix+sample_entry.length_low_word), 0
		ld	(ix+ sample_entry.length_low_word+1 ), 0
		ret

; End of function DoNextSampleBank


; =============== S U B	R O U T	I N E =======================================


zBankSwitch:				; CODE XREF: LoadSampleDatap
					; DoNextSampleBank+3p
		ld	b, 7
		ld	a, (ix+sample_entry.bank)


bankswitchloop:				; CODE XREF: zBankSwitch+9j
		ld	(zBankRegister), a
		rrca
		djnz	bankswitchloop

		xor	a
		ld	(zBankRegister), a
		ld	(zBankRegister), a
		ret

; End of function zBankSwitch

; ---------------------------------------------------------------------------
		ds 1F0Bh
sample0:	sample_entry <?>	; DATA XREF: EntryPoint+28o
					; EntryPoint:samplebankdoneo
driver_input:	ds 1			; DATA XREF: EntryPoint:PCMwaitloopr
					; EntryPoint+25w ...
fm_in_progress_flag:ds 1		; DATA XREF: EntryPoint+Cw
					; EntryPoint+21w ...
		ds 1
; end of 'ROM'

; ===========================================================================

; Segment type:	Regular
		segment	YM2612
		org 4000h
zYM2612_A0:	ds 1			; DATA XREF: EntryPoint:busywaitr
					; EntryPoint+18w ...
zYM2612_D0:	ds 1			; DATA XREF: EntryPoint+1Dw
					; EntryPoint+36o ...
zYM2612_A1:	ds 1
zYM2612_D1:	ds 1
; end of 'YM2612'

; ===========================================================================

; Segment type:	Regular
		segment	BankRegister
		org 6000h
zBankRegister:	ds 1			; DATA XREF: zBankSwitch:bankswitchloopw
					; zBankSwitch+Cw ...
; end of 'BankRegister'

; ===========================================================================

; Segment type:	Regular
		segment	SN76489
		org 7F11h
zPSG:		ds 1
; end of 'SN76489'

; ===========================================================================

; Segment type:	Regular
		segment	ROMWindow
		org 8000h
zROMWindow:	ds 8000h		; DATA XREF: DoNextSampleBank+6o
					; DoNextSampleBank+24o
; end of 'ROMWindow'


		end
